package managers;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import entities.Connection;

@Stateless
@LocalBean
public class ConnectionManager {
	@PersistenceContext
	EntityManager em;
	
	public Connection Create(Connection c){
		em.persist(c);
		em.flush();
		return c;
	}
	public Connection Update(Connection c){
		em.merge(c);
		em.flush();
		return c;
	}
	public Connection getConnection(String host, String target){
		return null;
	}
	public List<Connection> getConnections(String host){
		List<Connection> connections = new ArrayList<Connection>();
			
		TypedQuery<Connection> connectionQuery = em.createQuery("SELECT c FROM Connection c WHERE c.host = '"+ host +"'", Connection.class);
		for(Connection c : connectionQuery.getResultList()){
			connections.add(c);
		}
		return connections;
	}
}
